package com.lemner.ke.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.lemner.ke.R;

/**
 * 底部通用popupwindows
 *
 * @author 杨烨
 * @date 2018/9/12
 */
public class MyPopWindow extends PopupWindow {
    private View view;
    private Context context;
    private LinearLayout container;
    private int count = 0;

    public MyPopWindow(final Context context) {
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view = LayoutInflater.from(context).inflate(R.layout.pop_base, null);
        setContentView(view);
        container = view.findViewById(R.id.ll_container);
        this.context = context;
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
        setTouchable(true);
        setAnimationStyle(R.style.mypopwindow_anim_style);
        setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes();
                params.alpha = 1f;
                ((Activity) context).getWindow().setAttributes(params);
            }
        });
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void addButton(String text, View.OnClickListener onClickListener) {
        //加一条分割线
        if (count++ != 0) {
            View line = new View(context);
            line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.dp_1)));
            line.setBackgroundColor(0xffdedede);
            container.addView(line);
        }
        //添加新按钮
        Button button = new Button(context);
        button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.dp_52)));
        button.setText(text);
        button.setTextSize(0, context.getResources().getDimension(R.dimen.sp_14));
        button.setBackground(null);
        button.setOnClickListener(onClickListener);
        container.addView(button);
    }

    public void showAtLocation() {
        WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes();
        params.alpha = 0.5f;
        ((Activity) context).getWindow().setAttributes(params);
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }
}
